package com.acme.emailing;

import com.acme.exceptions.QueueException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Student on 21.11.2014.
 */
public class Queue {
    List<Email> queue = new ArrayList<Email>();
    private volatile boolean blocked = false;

    public void add(Email email) throws QueueException {
        if(blocked) throw new QueueException();
        synchronized (this) {
            queue.add(email);
            this.notifyAll();
        }
    }
    public Email getEmail() throws InterruptedException{
        synchronized (this) {
            while (true) {
                this.wait();
                if (queue.isEmpty()) continue;
                return this.queue.remove(0);
            }
        }
    }

    public  boolean isEmpty() {
        return  queue.isEmpty();
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }
}
